<?php

/**
 * adminchannels actions.
 *
 * @package    retailbrain
 * @subpackage adminchannels
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminchannelsActions extends sfActions {

    /**
     * Executes channels action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Canales');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Canales", "adminchannels/index"));

        $q = Doctrine_Query::create()
                ->from('Channels c')
                ->orderBy('c.name desc');

        $this->channels = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
    }

    /**
     * Executes editChannels action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditChannels(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Canal');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Canales", "adminchannels/index"), array("Editar Canal", "admin/editChannels?id=$id"));


        /** Channel * */
        $q = Doctrine_Query::create()
                ->from('Channels c')
                ->where('c.id_channel = ?', array($id))
                ->fetchArray();

        $this->channel = $q;
    }

    /**
     * Executes addChannels action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddChannels(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Canal');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Canales", "adminchannels/index"), array("Agregar Canal", "adminchannels/addChannels"));
    }

    /**
     * Executes createUser action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateChannel(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('channels');

        try {

            $tabla = Doctrine::getTable('Channels');
            $tabla = new Channels();

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/adminchannels/index');
    }

    /**
     * Executes updateChannel action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateChannel(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('channels');

        try {

            $tabla = Doctrine::getTable('Channels')->find(array($_arrayPost["id"]));

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminchannels/index');
    }

    /**
     * ChannelActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveChannel(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Channels c')
                ->set('c.active', '?', $active)
                ->where('c.id_channel = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Channels c')
                ->orderBy('c.name desc');

        $this->channels = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstChannels');
    }

    /**
     * ChannelDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteChannel(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Channels c')
                ->set('c.deleted', '?', $deleted)
                ->where('c.id_channel = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Channels c')
                ->orderBy('c.name desc');

        $this->channels = $q->fetchArray();
        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstChannels');
    }

}
